package com.curtainview.test.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Scroller;

import com.curtainview.test.R;
import com.curtainview.test.tools.BaseTools;

/**
 * Author: dengdayi
 * Date:2016/1/11 16:04
 * Description:窗帘布局
 */
public class CurtainView extends RelativeLayout {
    private static String TAG = "CurtainView";
    private Context mContext;
    //屏幕高度
    private int mScreenHeigh;
    //屏幕宽度
    private int mScreenWidth;
    //滑动类
    private Scroller mScroller;
    //图片：广告
    private ImageView mIvCurtainad;
    //广告幕布的高度
    private int curtainHeight;

    //点击时候Y的坐标
    private int downY = 0;

    //拖动时候Y的坐标
    private int moveY = 0;

    //拖动时候Y的方向距离
    private int scrollY = 0;

    //松开时候Y的坐标
    private int upY = 0;

    //是否打开
    private boolean isOpen = false;

    //是否在滚动
    private boolean isMove = false;

    //上升动画时间
    private int upDuration = 1000;
    //下落动画时间
    private int downDuration = 500;

    public CurtainView(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public CurtainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public CurtainView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    /**
     * 初始化View
     */
    private void initView() {
        //获取屏幕尺寸
        mScreenHeigh = BaseTools.getWindowHeigh(mContext);
        mScreenWidth = BaseTools.getWindowWidth(mContext);

        Interpolator mInterpolator = new LinearInterpolator();

        mScroller = new Scroller(mContext, mInterpolator);

        //将当前页面布局设置透明
//        setBackgroundColor(Color.argb(0, 0, 0, 0));

        //View布局
        View mView = LayoutInflater.from(mContext).inflate(R.layout.curtain_view, null);

        mIvCurtainad = (ImageView) mView.findViewById(R.id.img_curtain_ad);

        addView(mView);

        mIvCurtainad.post(new Runnable() {
            @Override
            public void run() {
                curtainHeight = mIvCurtainad.getHeight();
                Log.e(TAG, "curtainHeight:" + curtainHeight);

                scrollTo(0, curtainHeight);
            }
        });

        setOnTouchListener(mOnTouchListener);

    }

    @Override
    public void computeScroll() {
        //判断是否还在滚动，还在滚动为true
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            //更新界面
            postInvalidate();
            isMove = true;
        } else {
            isMove = false;
        }
        super.computeScroll();
    }

    private OnTouchListener mOnTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.e(TAG, "onTouch=======");
            if (!isMove) {
//                int offViewY = 0;//屏幕顶部和该布局顶部的距离
                switch (event.getAction()) {
                    //手指按下
                    case MotionEvent.ACTION_DOWN:
                        downY = (int) event.getRawY();
                        return true;
                    //手指移动
                    case MotionEvent.ACTION_MOVE:
                        moveY = (int) event.getRawY();
                        scrollY = moveY - downY;
                        //向上移动j
                        if (scrollY < 0) {
                            if (isOpen) {
                                //如果移动的距离小于mIvCurtainad的高度
//                                if (Math.abs(scrollY) <= curtainHeight) {
                                scrollBy(0, -scrollY);
//                                }
                            }
                        } else {
                            //向下移动
                            if (isOpen) {
//                                if (scrollY <= curtainHeight) {
                                scrollBy(0, curtainHeight - scrollY);
//                                scrollTo(0, curtainHeight - scrollY);
//                                }
                            }
                        }
                        break;
                    //手指松开
                    case MotionEvent.ACTION_UP:

                        upY = (int) event.getRawY();
                        if (Math.abs(upY - downY) < 10) {
                            onRopeClick();
                            break;
                        }
                        if (downY > upY) {
                            // 向上滑动
                            if (isOpen) {
                                if (Math.abs(scrollY) > curtainHeight / 2) {
                                    // 向上滑动超过半个屏幕高的时候 开启向上消失动画
                                    startMoveAnim(getScrollY(),
                                            (curtainHeight - getScrollY()), upDuration);
                                    isOpen = false;
                                } else {
                                    startMoveAnim(getScrollY(), -getScrollY(), upDuration);
                                    isOpen = true;
                                }
                            }
                        } else {
                            // 向下滑动
                            if (scrollY > curtainHeight / 2) {
                                // 向上滑动超过半个屏幕高的时候 开启向上消失动画
                                startMoveAnim(getScrollY(), -getScrollY(), upDuration);
                                isOpen = true;
                            } else {
                                startMoveAnim(getScrollY(), (curtainHeight - getScrollY()), upDuration);
                                isOpen = false;
                            }
                        }

                        break;
                }
            } else {

            }

            return false;
        }
    };

    /**
     * 拖动动画
     *
     * @param startY
     * @param dy       垂直距离, 滚动的y距离
     * @param duration 时间
     */
    public void startMoveAnim(int startY, int dy, int duration) {
        isMove = true;
        mScroller.startScroll(0, startY, 0, dy, duration);
        invalidate();//通知UI线程的更新
    }

    /**
     * 点击绳索开关，会展开关闭
     * 在onToch中使用这个中的方法来当点击事件，避免了点击时候响应onTouch的衔接不完美的影响
     */
    public void onRopeClick() {
        Log.e(TAG, "isOpen:" + isOpen);
        if (isOpen) {
            //说明当前页面已经移除
            CurtainView.this.startMoveAnim(0, curtainHeight, upDuration);
//            mIvCurtainad.setVisibility(GONE);
        } else {
            CurtainView.this.startMoveAnim(curtainHeight, -curtainHeight, downDuration);
//            mIvCurtainad.setVisibility(VISIBLE);
        }
        isOpen = !isOpen;
    }
}
